/**
 * Created by Administrator on 2017/3/11 0011.
 */
function setCookie(key, val, day, path, domain, secure) {
    // name=value;[expires=date];[path=路径];[domain=域名];[secure]
    var strCookie = "";
    strCookie =key+"="+val+";";
    if (day) { // 0== false  1true
        var date = new Date();
        date.setDate(date.getDate() + parseInt(day));
        strCookie=strCookie + "expires=" + date + ";";
    }

    if (path) {
        strCookie=strCookie + "path=/" + path + ";";
    }
    if (domain) {
        strCookie= strCookie + "domain=" + domain + ";";
    }
    if (secure) {
        if (Boolean(secure)) {
            strCookie=strCookie + "secure";
        } else {
            strCookie=strCookie
        }
    }
    document.cookie = strCookie;
}


function removeCookie(key, day) {
    var date = new Date();
    date.setDate(date.getDate() + parseInt(day))
    document.cookie = key + "= ;expires=" + date;
}


/**
 @method 获取所有的cookie
 @return Array(Object) 返回是数组里包对象
 * */
function getCookiesAll() {
    var arr = new Array();
    var strCookie = document.cookie; //获取了cookie
    if(strCookie.indexOf(";")!=-1) {
        var arr0 = strCookie.split(';');
        for (var j = 0; j < arr0.length; j++) {
            var obj = {};
            var arr1 = arr0[j].split('=');
            var arr2 = arr1[1].split("|");
            for (var i = 0; i < arr2.length; i++) {
                var arr3 = arr2[i].split(":");
                obj[arr3[0]] = arr3[1];
                if (i == arr2.length - 1) {
                    arr.push(obj);
                }
            }
        }
        }else{
            var obj = {};
            var arr1 = strCookie.split('=');
            var arr2 = arr1[1].split("|");
            for (var i = 0; i < arr2.length; i++) {
                arr3 = arr2[i].split(":");
                obj[arr3[0]] = arr3[1];
                if (i == arr2.length - 1) {
                    arr.push(obj);
                }
            }
        }
        return arr;
}

function getCookieByKey(key) {
    var aArr = getCookiesAll();
    for (var i = 0; i < aArr.length; i++) {
        for (var o in aArr[i]) {
            if (aArr[i][o].trim() == key.trim()) {
                return aArr[i][o];
            }
        }
    }
}
